package me.siphone.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import me.siphone.http.api.QueryModelApi;
import org.linphone.R;

public class TemplateAdapter extends RecyclerView.Adapter<TemplateAdapter.TemplateViewHolder> {

    private List<QueryModelApi.Model> modelList;
    private OnItemSelectedListener listener;

    public TemplateAdapter(List<QueryModelApi.Model> modelList) {
        this.modelList = modelList;
    }

    @NonNull
    @Override
    public TemplateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TemplateViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_template_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TemplateViewHolder holder, int position) {
        QueryModelApi.Model model = modelList.get(position);
        holder.ivType.setImageResource(model.getType() == 0 ? R.drawable.rain : R.drawable.wave);
        holder.tvType.setText("类型：" + (model.getType() == 0 ? "雨量" : "水位"));
        holder.tvTopic.setText("主题：" + model.getPublishTheme());
        holder.tvContent.setText("内容：" + model.getRemark());
        holder.itemView.setSelected(model.isSelected());
        holder.itemView.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = holder.getAdapterPosition();
                        for (int i = 0; i < modelList.size(); i++) {
                            modelList.get(i).setSelected(i == pos);
                            notifyItemChanged(i);
                        }

                        QueryModelApi.Model model = modelList.get(pos);
                        if (listener != null) {
                            listener.onItemSelected(model);
                        }
                    }
                });
    }

    @Override
    public int getItemCount() {
        return modelList.size();
    }

    static class TemplateViewHolder extends RecyclerView.ViewHolder {

        ImageView ivType;
        TextView tvType;
        TextView tvTopic;
        TextView tvContent;

        public TemplateViewHolder(@NonNull View itemView) {
            super(itemView);
            ivType = itemView.findViewById(R.id.iv_type);
            tvType = itemView.findViewById(R.id.tv_type);
            tvTopic = itemView.findViewById(R.id.tv_topic);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.listener = listener;
    }

    public interface OnItemSelectedListener {
        void onItemSelected(QueryModelApi.Model model);
    }
}
